//
//  DMApplyAppointmentController.m
//  CQKuaiYi
//
//  Created by ling on 17/1/9.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMApplyAppointmentController.h"
#import "DMApplyAppointmentCell.h"
#import "DMApplyAppointmentModel.h"
#import "DMDoctorDetailController.h"

static NSString *rid = @"cell";

@interface DMApplyAppointmentController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray<DMApplyAppointmentModel *> *doctorList;

@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@end

@implementation DMApplyAppointmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self loadData];
}

#pragma mark - tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DMDoctorDetailController *doctorDetail = [[DMDoctorDetailController alloc]init];
    
    DMApplyAppointmentModel *model = [_doctorList objectAtIndex:indexPath.row];
    
    doctorDetail.model = model;
    
    [self.navigationController pushViewController:doctorDetail animated:YES];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _doctorList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DMApplyAppointmentCell *cell = [tableView dequeueReusableCellWithIdentifier:rid forIndexPath:indexPath];
    
    DMApplyAppointmentModel *model = _doctorList[indexPath.row];
    
    cell.model = model;
    
    return cell;
}


#pragma mark - 加载数据
- (void)loadData
{
    
    //1. 创建url
    //返回当前的时间戳
    BOOL isPullUp = YES;
    
    //如果下拉刷新正在招待
    isPullUp = !_refreshHeader.isRefreshing;
    
    NSInteger currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime == 0) {
        currentTime = [[NSDate date] timeIntervalSince1970];
    }
    //定义
    NSString *URL = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php" ;
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};;
    //解析
    [DMApplyAppointmentModel getDoctorListDataWithURLString:URL andParameters:parameters andSuccessBlock:^(NSArray *newsList) {
        //赋值
        _doctorList = newsList;
        
        //       NSLog(@"%@",newsList);
        //主线程刷新
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [_tableView reloadData];
            
            //如果是上拉
            if (isPullUp == NO) [_refreshHeader endRefreshing];
        }];
    }];
}

#pragma mark - 搭建界面
- (void)setupUI
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.rowHeight = 75;
    
    [_tableView registerClass:[DMApplyAppointmentCell class] forCellReuseIdentifier:rid];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
